﻿<?xml version="1.0" encoding="utf-8"?>
<!-- 
    Jinx AJAX Framework
    Copyright (C) 2008 Blake Hegerle

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:n="urn:jinxweb-org:view-v1"
                xmlns:html="http://www.w3.org/1999/xhtml">
    <xsl:template match="*[@n:*]" mode="construction">
        <xsl:param name="path-root" />
        <xsl:param name="indent" />

        <xsl:variable name="control-name" select="@n:*[position() = 1]" />

        <xsl:variable name="control-parameters" select="@n:*[position() > 1]" />

        <xsl:value-of select="$indent"/>

        <xsl:text>new </xsl:text>
        <xsl:value-of select="local-name($control-name)" />
        <xsl:text>Control('</xsl:text>
        <xsl:value-of select="$control-name" />
        <xsl:text>', {</xsl:text>

        <xsl:for-each select="$control-parameters">
            <xsl:value-of select="local-name(.)"/>
            <xsl:text>: '</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>', </xsl:text>
        </xsl:for-each>

        <xsl:call-template name="prototype-children">
            <xsl:with-param name="indent" select="$indent" />
            <xsl:with-param name="path-root" select="$path-root" />
        </xsl:call-template>

        <xsl:if test="$path-root">
            <xsl:text>pathRoot: </xsl:text>
            <xsl:value-of select="$path-root"/>
            <xsl:text>, </xsl:text>
        </xsl:if>

        <xsl:text>tag: '</xsl:text>
        <xsl:value-of select="local-name(.)"/>
        <xsl:text>'</xsl:text>

        <xsl:text>})</xsl:text>
    </xsl:template>

    <xsl:template match="*" mode="custom-construction" />

    <xsl:template match="text()" mode="prototype">
        <xsl:param name="indent" />

        <xsl:variable name="text" select="normalize-space(.)"/>

        <xsl:if test="$text">
            <xsl:value-of select="$indent"/>

            <xsl:text>new TextControl('</xsl:text>
            <xsl:value-of select="$text" />
            <xsl:text>'),&#10;</xsl:text>
        </xsl:if>
    </xsl:template>

    <xsl:template match="*[@n:*]" mode="prototype">
        <xsl:param name="path-root" />
        <xsl:param name="indent" />

        <xsl:apply-templates select="." mode="construction">
            <xsl:with-param name="path-root" select="$path-root"/>
            <xsl:with-param name="indent" select="$indent" />
        </xsl:apply-templates>

        <xsl:text>,&#10;</xsl:text>
    </xsl:template>

    <xsl:template match="*" mode="prototype">
        <xsl:param name="path-root" />
        <xsl:param name="indent" />

        <xsl:value-of select="$indent"/>

        <xsl:text>new HtmlControl('</xsl:text>
        <xsl:value-of select="local-name(.)" />
        <xsl:text>', [&#10;</xsl:text>

        <xsl:apply-templates select="child::node()" mode="prototype">
            <xsl:with-param name="path-root" select="$path-root"/>
            <xsl:with-param name="indent" select="concat($indent, '    ')" />
        </xsl:apply-templates>

        <xsl:value-of select="$indent"/>

        <xsl:text>], {</xsl:text>
        <xsl:for-each select="@*[namespace-uri() = '']">
            <xsl:value-of select="local-name(.)"/>
            <xsl:text>: '</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>', </xsl:text>
        </xsl:for-each>
        <xsl:text>}),&#10;</xsl:text>
    </xsl:template>

    <xsl:template name="prototype-children">
        <xsl:param name="path-root" />
        <xsl:param name="indent" />

        <xsl:if test="* or child::text()">
            <xsl:text>prototypeFunction: function(pathRoot) { return [&#10;</xsl:text>

            <xsl:apply-templates select="child::node()" mode="prototype">
                <xsl:with-param name="path-root" select="'pathRoot'"/>
                <xsl:with-param name="indent" select="concat($indent, '    ')" />
            </xsl:apply-templates>

            <xsl:text>&#10;</xsl:text>
            <xsl:value-of select="$indent"/>
            <xsl:text>]; }, </xsl:text>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>
